package com.softWang.system.controller;
import com.softWang.common.aop.annotation.SysOperationLog;
import com.softWang.common.exception.LogicException;
import com.softWang.common.response.PageBean;
import com.softWang.common.request.Idparam;
import com.softWang.common.response.ResponseResult;
import com.softWang.common.utils.excel.ExcelUtils;
import com.softWang.system.constants.SysModel;
import com.softWang.system.controller.vo.systemUsers.*;
import com.softWang.system.po.SystemUsers;
import com.softWang.system.service.SystemUsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
@Validated
@RestController
@Api(value="用户管理",tags="用户管理")
@RequestMapping("/system/user")
public class SystemUsersController {
    @Resource
    private SystemUsersService userService;

    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @ApiOperation(value="查询用户列表")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public ResponseResult<PageBean<SystemUserQueryAllResult>> queryall(@RequestBody @Validated SystemUserQueryAllParam param, HttpServletRequest request) {
        List<SystemUserQueryAllResult> users=userService.queryall(param);
        PageBean page=new PageBean(users);
        return ResponseResult.success(page);
    }

    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @ApiOperation(value="添加用户")
    @SysOperationLog(value = "添加用户", model = SysModel.USER_MANAGE)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    //@Repeat(message = "重复提交")
    public ResponseResult<Long> add(@RequestBody @Validated SystemUsersAddParam param, HttpServletRequest request) throws Exception {
        SystemUserCheckNameParam nameparam = new SystemUserCheckNameParam();
        nameparam.setLoginName(param.getLoginName());
        Long userNum=userService.countByLoginName(nameparam);
        if(userNum>0){
            throw new LogicException("登录名已存在");
        }
        SystemUsers user = new SystemUsers();
        BeanUtils.copyProperties(param,user);
        user.setType(SystemUsers.UserType.ADMIN.getCode());
        user.setState(SystemUsers.UserState.NORMAL.getCode());
        Long userid = userService.add(user);
        return ResponseResult.success(userid);
    }

    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @ApiOperation(value="修改用户")
    @SysOperationLog(value = "修改用户", model = SysModel.USER_MANAGE)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResponseResult<Long> edit(@RequestBody @Validated SystemUsersEditParam param, HttpServletRequest request) throws Exception {
        SystemUserCheckNameParam nameparam = new SystemUserCheckNameParam();
        nameparam.setLoginName(param.getLoginName());
        nameparam.setId(param.getId());
        Long userNum=userService.countByLoginName(nameparam);
        if(userNum>0){
            throw new LogicException("登录名已存在");
        }
        SystemUsers user = new SystemUsers();
        BeanUtils.copyProperties(param,user);
        Long userid = userService.edit(user);
        return ResponseResult.success(userid);
    }

    @PreAuthorize("@ss.hasPermi('system:user:detail')")
    @ApiOperation(value="查看详情")
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public ResponseResult<SystemUserDetailResult> getById(@RequestBody @Validated Idparam idparam) {        ;
        return ResponseResult.success(userService.getById(idparam.getId()));
    }

    @PreAuthorize("@ss.hasPermi('system:user:del')")
    @ApiOperation(value="删除用户")
    @SysOperationLog(value = "删除用户", model = SysModel.USER_MANAGE)
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ResponseResult del(@RequestBody @NotEmpty(message = "ids 不能为空") List<@NotNull(message = "ids 不能为空") Long> ids, HttpServletRequest request) {
        userService.del(ids);
        return ResponseResult.success();
    }

    @ApiOperation(value="检查登录名是否可用")
    @RequestMapping(value = "/checkName", method = RequestMethod.POST)
    public ResponseResult<Boolean> checkName(@RequestBody @Validated SystemUserCheckNameParam nameparam) {
        Long userNum=userService.countByLoginName(nameparam);
        if(userNum==0){
            return ResponseResult.success(true);
        }else{
            return ResponseResult.success(false);
        }
    }

    @PreAuthorize("@ss.hasPermi('system:user:reset')")
    @ApiOperation(value="重置密码")
    @SysOperationLog(value = "重置密码", model = SysModel.USER_MANAGE)
    @RequestMapping(value = "/initPassword",method = RequestMethod.POST)
    public ResponseResult initPassword(@RequestBody @Validated Idparam idparam) {
        userService.initPassword(idparam.getId());
        return ResponseResult.success();
    }

    @ApiOperation(value="修改密码")
    @SysOperationLog(value = "修改密码", model = SysModel.USER_MANAGE)
    @RequestMapping(value = "/changePassword",method = RequestMethod.POST)
    public ResponseResult changePassword(@RequestBody @Validated Idparam idparam) {
        userService.initPassword(idparam.getId());
        return ResponseResult.success();
    }

    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @ApiOperation(value="导出测试")
    @RequestMapping(value = "/export",method = RequestMethod.POST)
    public void export(@RequestBody @Validated SystemUserQueryAllParam param,HttpServletResponse response) throws Exception {
        List<SystemUserExportResult> exportResults = userService.export(param);
        ExcelUtils.write(response, "用户信息.xls", "用户信息", SystemUserExportResult.class, exportResults);
    }



}
